export default class ResizeManager {

    static manager: ResizeManager;

    public eventArr: Array<resizeData> = [];

    public constructor() {
        ResizeManager.manager = this;
        window.onresize = this.resizeEvent;
    }

    public static add(id: string, action: Function) {
        const { eventArr } = ResizeManager.manager;
        let actions = eventArr.find(u => u.id == id)?.actions;
        if (!actions) {
            actions = [];
            eventArr.push({ id: id, actions: actions });
        }
        actions.push(action);
    }

    public static del(id: string) {
        const { eventArr } = ResizeManager.manager;
        for (var i = 0; i < eventArr.length; i++) {
            if (eventArr[i].id == id) {
                eventArr.splice(i, 1);
            }
        }
    }

    private resizeEvent() {
        const { eventArr } = ResizeManager.manager;
        eventArr.forEach(data => {
            data.actions.forEach(action => {
                action();
            })
        });
    }
}

interface resizeData {
    id: string,
    actions: Array<Function>,
}